package test;

import java.util.Date;

public class Fee {

    public static void main(String[] args) throws Exception {
        Fee fee1 = new Fee(0, 3, 14, 6, 17, 0);
        Fee fee2 = new Fee(3, 10, 2.5, 6, 17, 1);
        Fee fee3 = new Fee(10, 10, 3.5, 6, 17, 1);
        fee1.setFee(fee2);
        fee2.setFee(fee3);

        System.out.println(fee1.getFee(new Date(), 1, 0));
        System.out.println(fee1.getFee(new Date(), 2, 0));
        System.out.println(fee1.getFee(new Date(), 5, 0));
        System.out.println(fee1.getFee(new Date(), 7, 0));
        System.out.println(fee1.getFee(new Date(), 9, 0));
        System.out.println(fee1.getFee(new Date(), 11, 0));
        System.out.println(fee1.getFee(new Date(), 15, 0));
    }

    private double distanceL;// 起始路程
    private double distanceR;// 结束路程
    private double price;// 价格，根据temp来表示
    private int timeL;// 起始时间
    private int timeR;// 结束时间
    private int temp;// 0,表示固定价，1表示按公里数计费
    private double sum;// 当前的费用

    private Fee fee;// 下一个收费节点

    public double getFee(Date time, double distance, double sum) {

        if (!isInDate(time)) {
            if (null == fee) {
                return sum;
            }
            return fee.getFee(time, distance, sum);
        }

        double t = distanceR;
        if (distance < distanceR && distance >= distanceL) {
            t = distance;
        }
        if (distanceR == distanceL && distance >= distanceR) {
            t = distance;
        }

        if (temp == 0) {
            sum = sum + price;
        } else {
            sum = sum + (t - distanceL) * price;
        }
        if (null == fee) {
            return sum;
        }
        return fee.getFee(time, distance, sum);
    }

    @SuppressWarnings("deprecation")
    public boolean isInDate(Date nowTime) {
        int now = nowTime.getHours();

        if (timeL < timeR) {
            if (now >= timeL && now < timeR) {
                return true;
            }
            return false;
        } else {
            if (now >= timeR && now < timeL) {
                return false;
            }
            return true;
        }

    }

    public Fee(double distanceL, double distanceR, double price, int timeL,
               int timeR, int temp) {
        super();
        this.distanceL = distanceL;
        this.distanceR = distanceR;
        this.price = price;
        this.timeL = timeL;
        this.timeR = timeR;
        this.temp = temp;
    }

    public Fee(double distanceL, double distanceR, double price, int timeL,
               int timeR, int temp, Fee fee) {
        super();
        this.distanceL = distanceL;
        this.distanceR = distanceR;
        this.price = price;
        this.timeL = timeL;
        this.timeR = timeR;
        this.temp = temp;
        this.fee = fee;
    }

    public double getDistanceL() {
        return distanceL;
    }

    public void setDistanceL(double distanceL) {
        this.distanceL = distanceL;
    }

    public double getDistanceR() {
        return distanceR;
    }

    public void setDistanceR(double distanceR) {
        this.distanceR = distanceR;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getTimeL() {
        return timeL;
    }

    public void setTimeL(int timeL) {
        this.timeL = timeL;
    }

    public int getTimeR() {
        return timeR;
    }

    public void setTimeR(int timeR) {
        this.timeR = timeR;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }

    public double getSum() {
        return sum;
    }

    public void setSum(double sum) {
        this.sum = sum;
    }

    public Fee getFee() {
        return fee;
    }

    public void setFee(Fee fee) throws Exception {
        this.fee = fee;
    }


}
